# basic3d.py
# Copyright (C) 2007 Sergio Perez Camacho - BRUE
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


import os, os.path
import soya
import basicsfx

import soya.label3d

import pygame

time = 0.0
ii = [0,0,0,0,0]

GR = 0.5
p = 0


""" class BasicElement
    This class represents one playable beat """
class BasicElement(soya.Body):
    def __init__(self, blenderfile,scene, x,y,z, s, mObj, inst):
        global ii
        model = soya.Model.get(blenderfile)        
        self.x = x
        self.y = y
        self.zp = z/s.bpm
        soya.Body.__init__(self, scene, model)
        self.song = s
        self.mObj = mObj
        self.inst = inst
        ii[inst] = mObj

   
    def begin_round(self):
        global time
        pass
            
    def advance_time(self,proportion):
        global time
        global ii
        if self.z > 0:
            self.y = self.y - 0.3
        if (self.z > 5) & (ii[self.inst] < len(self.song.beats[self.inst])-1):
            self.y = -4
            self.zp = -self.song.beats[self.inst][ii[self.inst]]/(self.song.bpm)            
            ii[self.inst] = ii[self.inst] + 1
            print "->" + str(ii[self.inst]) + "zp: " + str(self.zp)
        self.z = self.zp + time/(self.song.bpm)
        #soya.Body.advance_time(self, proportion)
    



class Plane(soya.Body):
    def __init__(self, blenderfile,scene, x, y, z, rBeats):
        model = soya.Model.get(blenderfile)        
        self.x = x
        self.y = y
        self.z = z
        soya.Body.__init__(self, scene, model)
        self.rBeats = rBeats
        self.points = soya.label3d.Label3D(scene, "0")
        self.points.set_xyz(-5.0,4.0,0.0)
        self.points.lit = 0
        
    def proccess(self,proportion):
        # Here we should proccess the object
        global time
        global p
        time = float(pygame.mixer.music.get_pos())
        if (time == -1): soya.MAIN_LOOP.stop()
        for event in soya.process_event():  
            if event[0] == soya.sdlconst.KEYDOWN:
                if (event[1] == soya.sdlconst.K_q):
                    for c in self.rBeats:
                        if (c.z > -GR) & (c.z < GR) & (c.inst==0):
                            p = p + 1
                            c.y = 1
                if (event[1] == soya.sdlconst.K_w):
                    for c in self.rBeats:
                        if (c.z > -GR) & (c.z < GR) & (c.inst==1):
                            p = p + 1
                            c.y = 1
                if (event[1] == soya.sdlconst.K_e):
                    for c in self.rBeats:
                        if (c.z > -GR) & (c.z < GR) & (c.inst==2):
                            p = p + 1
                            c.y = 1
                if (event[1] == soya.sdlconst.K_r):
                    for c in self.rBeats:
                        if (c.z > -GR) & (c.z < GR) & (c.inst==3):
                            p = p + 1
                            c.y = 1
                if (event[1] == soya.sdlconst.K_t):
                    for c in self.rBeats:
                        if (c.z > -GR) & (c.z < GR) & (c.inst==4):
                            p = p + 1
                            c.y = 1
                if event[1] == soya.sdlconst.K_ESCAPE:
                    soya.MAIN_LOOP.stop()
        self.points.text = str(p)
    
    def advance_time(self, proportion):
        soya.Body.advance_time(self, proportion)
        self.proccess(proportion)

class Instrument(soya.Body):
    def __init__(self, blenderfile,scene, x, y, z):
        model = soya.Model.get(blenderfile)        
        self.x = x
        self.y = y
        self.z = z
        soya.Body.__init__(self, scene, model)
        
        
    def proccess(self,proportion):
        # Here we should proccess the object
        pass
    
    def advance_time(self, proportion):
        soya.Body.advance_time(self, proportion)
        self.proccess(proportion)

class Drummer:
    def __init__(self, lBeats, plane, instrument, scene):

        self.lBeats = lBeats         # The list of beats
        
        self.plane = plane           # The plane under the beats

        self.instrument = instrument # The instrument at the end of
                                     # the plane
                                    
